package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.vo.RemainReservationCountVo;
import com.zzyl.nursing.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.ReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-06
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService
{

    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询当天取消预约的次数
     * @param userId
     */

    @Override
    public Integer getCancelledReservationCount(Long userId) {
        //获取当前的系统时间，并获取前面的年月日部分，一天中开始日子就是前面的部分加上00:00:00，结束日子就是开始日子+1 注意前包后不包
        long timeMillis = System.currentTimeMillis();
        LocalDateTime localDateTime = LocalDateTimeUtil.of(timeMillis);
        LocalDateTime startOfDay = localDateTime.toLocalDate().atStartOfDay();
        LocalDateTime endOfDay = startOfDay.plusDays(1);

        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Reservation::getStatus, 2)
                .eq(Reservation::getUpdateBy, userId)
                .ge(Reservation::getUpdateTime,startOfDay)
                .lt(Reservation::getUpdateTime,endOfDay);
        Long count = reservationMapper.selectCount(wrapper);
        return Convert.toInt(count);
    }

    /**
     * 查询每一个时间段内剩余预约次数:一个时间段内只能预约6个人
     * @param time
     * @return
     */

    @Override
    public List<RemainReservationCountVo> getRemainCount(long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        LocalDateTime startOfDay = localDateTime.toLocalDate().atStartOfDay();
        LocalDateTime endOfDay = startOfDay.plusDays(1);
        List<RemainReservationCountVo> list = reservationMapper.selectTimeAndCount(startOfDay,endOfDay);
        return list;
    }

    /**
     * 添加预约信息
     * @param reservationDto
     */
    @Override
    public void add(ReservationDto reservationDto) {
        Reservation reservation = new Reservation();
        reservation.setStatus(0);
        BeanUtils.copyProperties(reservationDto,reservation);
        this.save(reservation);
    }

    @Override
    public List<ReservationVo> selectByPage(int status) {
        List<ReservationVo> reservationVos = reservationMapper.selectByPage(status);

        return reservationVos;
    }

    @Override
    public void cancel(long id) {
        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setStatus(2);
        reservationMapper.updateById(reservation);
    }
}
